
local color        = require "util.color"

-- 伤害显示
local locplayer = ac.player.self
ac.game:event '单位-伤害显示' (function(_, source,damage)
    if damage.last_damage < 1 then
        return
    end
    if damage.dot or damage.aoe then
        return
    end

    local show = true
    local dmg = damage.last_damage
    local damage_type = damage.damage_type
    local target = damage.target
    local player = source.owner
    local data = player:get_data('伤害显示UI') or {}
    local info = data[damage_type] or {}
    data[damage_type] = info
    player:set_data('伤害显示UI',data)
    -- 被攻击单位对攻击单位不可见
    if not source:is_visible(player) or not player:is_online() then
        return
    end

    
    if not locplayer:get_data('开关-友伤显示',true) and player~=locplayer then
        show = false
    end
    -- 被攻击单位对攻击单位不可见
    if not locplayer:get_data('开关-伤害显示',true) then
        show = false
    end
    
    --local jump = 0
    -- local key = damage.damage_type .. '次数'
    -- local count = player:get_data(key,0)  + 1
    -- player:set_data(key,count)
    local skill = damage.skill
    if damage_type~='真实' then
        local ui = info[target.handle]
        if ui==nil then
            ui = ac.ui_tag{
                text = '',
                damage = 0,
                font_size = 16,
                target = target:get_point(),
                angle = 90,
                speed = 120,
                life = 0.5,
                fade = 0.25,
                jump_rate = 1,
                jump_time = 0.5,
            }
            if damage_type=='魔法' then
                --ui:move(90,80)
                ui:set_color(0xff00ccff)
            else
                --ui:move(90,80)
                ui:set_color(0xffff6600)
            end
            -- info[target.handle] = ui
            -- ac.wait(ui.jump_time*1000,function()
            --     info[target.handle] = nil
            -- end)
        end
        ui.damage = ui.damage + dmg

        local text = format_number1(ui.damage)
        if damage['暴击'] then
            ui['暴击'] = true
        end
        if ui['暴击'] then
            text = ('\u{2460}%s'):format(text)
        end
        if show==false then
            text = ''
        end
        ui:set_text(text)
        -- ac.ui_tag {
        --     string = text,
        --     speed = 200,
        --     angle = angle,
        --     life = 2,
        --     fade = 1,
        --     position = target:get_point(),
        -- }
    end
end)

